﻿using HomagChina.Basic.CCT.Tools;
using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using localText = Proxy.Properties.Resources;

namespace Proxy
{
    public class DealWithFiles
    {
        Mes2CCTCsvAnalyzer mes2CCTCsvAnalyzer = new Mes2CCTCsvAnalyzer();
        private readonly string csv_Source = ConfigurationManager.AppSettings["CsvPath"];
        private string csv_HistoryPath = ConfigurationManager.AppSettings["CsvHistoryPath"];
        private readonly string mpr_Source = ConfigurationManager.AppSettings["MprPath"];
        private readonly string mprTargetPath_H330_1 = ConfigurationManager.AppSettings["MprTargetPath_H330_1"];
        private readonly string mprTargetPath_H330_2 = ConfigurationManager.AppSettings["MprTargetPath_H330_2"];
        public DealWithFiles()
        {
            Task.Factory.StartNew(() => DealWithBoardInfoFilesByAuto());
            Task.Factory.StartNew(() => DealWithMPRFilesByAuto());
        }

        private void DealWithMPRFilesByAuto()
        {
            do
            {
                Thread.Sleep(1000 * 60);
                try
                {
                    string[] files = Directory.GetFiles(mpr_Source);
                    //存在待处理的批次文件
                    if (files.Length > 0)
                    {
                        for (int i = 0; i < files.Length; i++)
                        {
                            var mprName = files.FirstOrDefault(s => s.EndsWith(".mpr"));
                            if (mprName == null)
                            {
                                return;
                            }
                            var targetPath = mprTargetPath_H330_1 + mprName.Split('\\').Last().ToString();
                            if (File.Exists(targetPath))
                            {
                                File.Delete(targetPath);
                            }
                            File.Copy(mprName, targetPath);

                            targetPath = mprTargetPath_H330_2 + mprName.Split('\\').Last().ToString();
                            if (File.Exists(targetPath))
                            {
                                File.Delete(targetPath);
                            }
                            File.Copy(mprName, targetPath);

                            File.Delete(mprName);
                        }
                    }
                }
                catch (Exception ex)
                {
                    SerilogHandle.Logger_DealWithCsv.Error(ex.Message);
                }
            } while (true);
        }

        /// <summary>
        /// 单层级文件夹
        /// </summary>
        /// <param name="Source"></param>
        public void DealWithBoardInfoFilesByAuto()
        {
            do
            {
                Thread.Sleep(1000 * 60);
                try
                {
                    string[] files = Directory.GetFiles(csv_Source);
                    //存在待处理的批次文件
                    if (files.Length > 0)
                    {
                        var csvName = files.FirstOrDefault(s => s.EndsWith(".csv"));
                        if (csvName == null)
                        {
                            return;
                        }
                        csv_HistoryPath = csv_HistoryPath + csvName.Split('\\').Last().ToString();
                        if (mes2CCTCsvAnalyzer.DealWithMes2CCTCsvFile(csvName, ';'))
                        {
                            if (File.Exists(csv_HistoryPath))
                            {
                                File.Delete(csv_HistoryPath);
                            }
                            File.Move(csvName, csv_HistoryPath);
                        }
                    }
                }
                catch (Exception ex)
                {
                    SerilogHandle.Logger_DealWithCsv.Error(ex.Message);
                }
            } while (true);
        }
    }
}